package com.woparker.kiosk.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;


import com.woparker.kiosk.bean.NodeBean;
import com.woparker.kiosk.utils.CommonUtils;
import com.woparker.kiosk.utils.SearchTask;

import java.util.Collections;
import java.util.List;


/**
 * Created by yuanxd on 2017/9/25 0025.
 */

public class MapViewDraw {
    private BubbleView startBubbleView; // 起点气泡
    private BubbleView stopBubbleView; // 终点气泡
    private Paint panP = new Paint();
    private Paint panL = new Paint();
    //贝塞尔曲线画笔
    private Paint mGesturePaint = new Paint();
    // 第三层游戏碰撞物理层数组
    Bitmap mBitmap = null;
    // 游戏画笔
    Paint mPaint = null;
    // 横向纵向tile块的数量
    int mWidthTileCount = 0;
    int mHeightTileCount = 0;
    //背景图片
    private Bitmap bgBitmap;
    //人物位置
    private Bitmap startPeopleBitmap;
    //终点图标
    private Bitmap endBitmap;
    //查询工具类
    private SearchTask searchTask;

    // tile块的宽高
    public static float TILE_WIDTH = 5;
    //路径集合
    private Path mPath = new Path();

    // 画布
    private Canvas fCanvas;
    private QSView qsView;

    private Context context;

    //起点X坐标
    private int srcX;
    //起点Y坐标
    private int srcY;

    //终点X坐标
    private int tagX;
    //终点Y坐标
    private int tagY;

    //位置描述
    private String locationDesc;
    //位置信息
    private String locationStr;
    //终点标题
    private String stopTitle;
    //终点描述
    private String stopStr;


    public Path getmPath() {
        return mPath;
    }

    /**
     * @param context           上下文对象
     * @param canvas            画布
     * @param qsView            消息句柄
     * @param bgBitmap          背景图片
     * @param startPeopleBitmap 起点人物图片
     * @param endBitmap         终点图标
     * @param searchTask        查询数据对象
     * @param srcX              起点X
     * @param srcY              起点Y
     * @param tagX              终点X
     * @param tagY              终点Y
     * @param locationDesc      起点位置标题
     * @param locationStr       起点位置描述
     * @param stopTitle         终点位置标题
     * @param stopStr           终点位置描述
     */
    public MapViewDraw(Context context, Canvas canvas, QSView qsView, Bitmap bgBitmap,
                       Bitmap startPeopleBitmap, Bitmap endBitmap,
                       SearchTask searchTask, int srcX, int srcY, int tagX, int tagY, String locationDesc,
                       String locationStr, String stopTitle, String stopStr) {
        this.context = context;
        this.fCanvas = canvas;
        this.qsView = qsView;
        this.bgBitmap = bgBitmap;
        this.startPeopleBitmap = startPeopleBitmap;
        this.endBitmap = endBitmap;
        this.searchTask = searchTask;
        this.srcX = srcX;
        this.srcY = srcY;
        this.tagX = tagX;
        this.tagY = tagY;
        this.locationDesc = locationDesc;
        this.locationStr = locationStr;
        this.stopTitle = stopTitle;
        this.stopStr = stopStr;
        mPaint = new Paint();
        panP.setColor(Color.RED);// 设置红色
        panP.setStrokeWidth(2);
        panP.setStyle(Paint.Style.FILL);
        panP.setAntiAlias(true);
        panL.setColor(Color.BLUE);// 设置蓝色
        panL.setStrokeWidth(2);


        mGesturePaint.setAntiAlias(true);
        mGesturePaint.setStyle(Paint.Style.STROKE);
        mGesturePaint.setStrokeWidth(5);
        mGesturePaint.setColor(Color.BLUE);

    }

    public void onChangeDraw(Canvas canvas) {
        DrawMap(fCanvas, mPaint, mBitmap);
    }


    public Canvas getfCanvas() {
        return fCanvas;
    }

    /**
     * 清除路线
     */
    public void clearnBg(){
        // 画背景层
        fCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        fCanvas.drawBitmap(bgBitmap, 0, 0, null);
        qsView.onMapInit();
    }

    public Bitmap getBgBitmap() {
        return bgBitmap;
    }

    private void DrawMap(Canvas canvas, Paint paint, Bitmap bitmap) {
        // 画背景层
        canvas.drawBitmap(bgBitmap, 0, 0, null);
        List<NodeBean> nodeList = searchTask.getBestRoot();
        int size = nodeList.size();
        if (size > 0) {
            // 集合数据倒叙
            Collections.reverse(nodeList);
            NodeBean mStartPoint = nodeList.get(0);
            float startX = TILE_WIDTH * mStartPoint.y + TILE_WIDTH / 2;
            float starty = TILE_WIDTH * mStartPoint.x + TILE_WIDTH / 2;
            mPath.moveTo(startX , starty);
            for (NodeBean node : nodeList) {
                final float x = TILE_WIDTH * node.y + TILE_WIDTH / 2;
                final float y = TILE_WIDTH * node.x + TILE_WIDTH / 2;

                final float previousX = TILE_WIDTH * mStartPoint.y + TILE_WIDTH / 2;
                final float previousY = TILE_WIDTH * mStartPoint.x + TILE_WIDTH / 2;

                final float dx = Math.abs(x - previousX);
                final float dy = Math.abs(y - previousY);
                //两点之间的距离大于等于3时，生成贝塞尔绘制曲线
                if (dx >= 3 || dy >= 3) {
                    //设置贝塞尔曲线的操作点为起点和终点的一半
                    float cX = (x + previousX) / 2;
                    float cY = (y + previousY) / 2;
                    //二次贝塞尔，实现平滑曲线；previousX, previousY为操作点，cX, cY为终点

                    mPath.quadTo(TILE_WIDTH * mStartPoint.y + TILE_WIDTH / 2, TILE_WIDTH * mStartPoint.x + TILE_WIDTH / 2, cX, cY);
                    //第二次执行时，第一次结束调用的坐标值将作为第二次调用的初始坐标值
                    mStartPoint = node;
                }
            }
            //关闭当前轮廓，若当前的点不等于第一点，则自动连接第一个点
            canvas.drawPath(mPath, mGesturePaint);
        }

        if (size > 0) {
            //画起点图标
            canvas.drawBitmap(startPeopleBitmap, TILE_WIDTH * nodeList.get(0).y + TILE_WIDTH / 2 - startPeopleBitmap.getWidth() / 2,
                    TILE_WIDTH * nodeList.get(0).x + TILE_WIDTH / 2 - startPeopleBitmap.getHeight() / 2, paint);
            //画终点图标
            canvas.drawBitmap(endBitmap, TILE_WIDTH * nodeList.get(size - 1).y + TILE_WIDTH / 2 - endBitmap.getWidth() / 2,
                    TILE_WIDTH * nodeList.get(size - 1).x + TILE_WIDTH / 2 - endBitmap.getHeight(), paint);

            //画起点气泡
            startBubbleView = new BubbleView(context);
            startBubbleView.setBubbleText(locationDesc, locationStr);
            Bitmap startBuddle = CommonUtils.getCache(startBubbleView);
            canvas.drawBitmap(startBuddle, TILE_WIDTH * nodeList.get(0).y - startBuddle.getWidth() / 2,
                    TILE_WIDTH * nodeList.get(0).x - startBuddle.getHeight() - startPeopleBitmap.getHeight() / 2, paint);


            //画终点气泡
            stopBubbleView = new BubbleView(context);
            stopBubbleView.setBubbleText(stopTitle, stopStr);
            Bitmap stopBuddle = CommonUtils.getCache(stopBubbleView);
            canvas.drawBitmap(stopBuddle, TILE_WIDTH * nodeList.get(size - 1).y - stopBuddle.getWidth() / 2,
                    TILE_WIDTH * nodeList.get(size - 1).x - startBuddle.getHeight() - endBitmap.getHeight(), paint);
        }


        if (size <= 0) {
            //画起点气泡
            canvas.drawBitmap(startPeopleBitmap, srcX - startPeopleBitmap.getWidth() / 2,
                    srcY - startPeopleBitmap.getHeight() / 2, paint);
            startBubbleView = new BubbleView(context);
            startBubbleView.setBubbleText(locationDesc, locationStr);
            Bitmap startBuddle = CommonUtils.getCache(startBubbleView);
            canvas.drawBitmap(startBuddle, srcX - startBuddle.getWidth() / 2,
                    srcY - startBuddle.getHeight() - startPeopleBitmap.getHeight() / 2, paint);

            //画终点气泡
            canvas.drawBitmap(endBitmap, tagX - endBitmap.getWidth() / 2,
                    tagY - endBitmap.getHeight() / 2, paint);
            stopBubbleView = new BubbleView(context);
            stopBubbleView.setBubbleText(stopTitle, stopStr);
            Bitmap stopBuddle = CommonUtils.getCache(startBubbleView);
            canvas.drawBitmap(stopBuddle, tagX - stopBuddle.getWidth() / 2,
                    tagY - stopBuddle.getHeight() - endBitmap.getHeight(), paint);


        }
        //主fragment更新UI
        qsView.onMapInit();
    }
}
